<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src=https://unpkg.com/axios/dist/axios.min.js></script>
</head>

<body>
    <h1>注册</h1>
    邮箱 <input type="text" id="email" value=""><br>
    账号 <input type="text" id="usename"><br>
    密码 <input type="password" id="pwd"><br>
    头像<input type="file" id="headpic"><br>
    验证码<input type="text" id="verify"><span id="verifybox"></span><br>
    <button onclick="setRegister()">注册</button>
    <script>
        var verifybox = document.getElementById("verifybox")
        verifybox.onclick = getverify
        //验证码
        function getverify() {
            var url = "http://localhost:7001/verify"
            axios(url).then((res) => {
                // console.log(res.data);
                verifybox.innerHTML = res.data
            })
        }
        getverify()

        var email = document.getElementById("email")
        var usename = document.getElementById("usename")
        var pwd = document.getElementById("pwd")
        var headpic = document.getElementById("headpic")
        var verify = document.getElementById("verify")
        async function setRegister() {
            //表单验证
            //获取用户表单数据
            //发送给后端
            const formdata = new FormData()
            formdata.append("email", email.value)
            formdata.append("usename", usename.value)
            formdata.append("pwd", pwd.value)
            formdata.append("headpic", headpic.files[0])
            formdata.append("verify", verify.value)
            var url = "/register"
            let resdata = await axios.post(url, formdata)
            console.log(resdata,11111111111);
            if(resdata.data.code==4001){
                verify.style.border='red 1px solid'
            }else if(resdata.data.code==4002){
                alert(resdata.data.info)
            }else{
                window.location.href="https://www.baidu.com/"
            }
        }
    </script>
</body>

</html>